import { Context } from 'koa';

/**
 * 标准API响应格式
 */
export interface ApiResponse<T = any> {
  success: boolean;
  message: string;
  data?: T;
  code?: string;
  timestamp?: string;
}

/**
 * 分页响应格式
 */
export interface PaginatedResponse<T = any> extends ApiResponse<T[]> {
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

/**
 * 响应工具类
 */
export class ResponseUtil {
  /**
   * 成功响应
   */
  static success<T>(ctx: Context, data?: T, message = '操作成功', code = 'SUCCESS'): ApiResponse<T> {
    const response: ApiResponse<T> = {
      success: true,
      message,
      data,
      code,
      timestamp: new Date().toISOString()
    };
    return response;
  }

  /**
   * 创建成功响应
   */
  static created<T>(ctx: Context, data?: T, message = '创建成功', code = 'CREATED'): ApiResponse<T> {
    const response: ApiResponse<T> = {
      success: true,
      message,
      data,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = 201;
    return response;
  }

  /**
   * 错误响应
   */
  static error(ctx: Context, message = '操作失败', code = 'ERROR', statusCode = 400): ApiResponse {
    const response: ApiResponse = {
      success: false,
      message,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = statusCode;
    return response;
  }

  /**
   * 未找到响应
   */
  static notFound(ctx: Context, message = '资源未找到', code = 'NOT_FOUND'): ApiResponse {
    const response: ApiResponse = {
      success: false,
      message,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = 404;
    return response;
  }

  /**
   * 未授权响应
   */
  static unauthorized(ctx: Context, message = '未授权访问', code = 'UNAUTHORIZED'): ApiResponse {
    const response: ApiResponse = {
      success: false,
      message,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = 401;
    return response;
  }

  /**
   * 禁止访问响应
   */
  static forbidden(ctx: Context, message = '禁止访问', code = 'FORBIDDEN'): ApiResponse {
    const response: ApiResponse = {
      success: false,
      message,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = 403;
    return response;
  }

  /**
   * 服务器错误响应
   */
  static serverError(ctx: Context, message = '服务器内部错误', code = 'SERVER_ERROR'): ApiResponse {
    const response: ApiResponse = {
      success: false,
      message,
      code,
      timestamp: new Date().toISOString()
    };
    ctx.status = 500;
    return response;
  }

  /**
   * 分页响应
   */
  static paginated<T>(
    ctx: Context,
    data: T[],
    page: number,
    limit: number,
    total: number,
    message = '获取成功',
    code = 'SUCCESS'
  ): PaginatedResponse<T> {
    const totalPages = Math.ceil(total / limit);
    const response: PaginatedResponse<T> = {
      success: true,
      message,
      data,
      code,
      timestamp: new Date().toISOString(),
      pagination: {
        page,
        limit,
        total,
        totalPages
      }
    };
    return response;
  }
}